package com.ydsungan.controller;


import com.ydsungan.pojo.*;
import com.ydsungan.service.CommunityService;
import com.ydsungan.service.UserService;
import com.ydsungan.common.JSONResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(path = "/community")
@Api(value = "社区相关",tags = {"与考研社区页面相关的接口"})
@CrossOrigin
public class CommunityController extends BasicController{

    @Autowired
    private CommunityService communityService;

    @Autowired
    private UserService userService;

    @ApiOperation(value="获取帖子",notes = "获取几条文章")
    @RequestMapping(value = "/post", method = RequestMethod.GET)
    public JSONResult getTopPosts() throws Exception{
        List<PostVO> topPostList = new ArrayList<>();
        PostExample postExample = new PostExample();
        List<Post> resList = communityService.selectByExample(postExample);
        for(int i = 0; i < resList.size(); ++i){
            PostVO postVO = new PostVO();
            BeanUtils.copyProperties(resList.get(i), postVO);
            User user = userService.selectById(resList.get(i).getAuthor());
            postVO.setAuthorName(user.getNickname());
            postVO.setAvatarImg(user.getAvatar());
            postVO.setOpenId(user.getId());

            CommentsExample commentsExample = new CommentsExample();
            CommentsExample.Criteria criteria2 = commentsExample.createCriteria();
            criteria2.andPostIdEqualTo(resList.get(i).getId());
            List<Comments> commentsList = communityService.selectCommentsByExample(commentsExample);
            postVO.setCommentsNum(commentsList.size());
            List<CommentsVO> commentsVOList = new ArrayList<>();
            for(int j = 0; j < commentsList.size(); ++j){
                CommentsVO commentsVO = new CommentsVO();
                BeanUtils.copyProperties(commentsList.get(j), commentsVO);
                User commentUser = userService.selectById(commentsList.get(j).getUserId());
                commentsVO.setUserName(commentUser.getNickname());
                commentsVO.setUserAvatarUrl(commentUser.getAvatar());
                commentsVO.setLiked(0);
                commentsVOList.add(commentsVO);
            }
            postVO.setCommentsList(commentsVOList);
            topPostList.add(postVO);
        }


        return JSONResult.ok(topPostList);
    }
    @ApiOperation(value="获取文章",notes = "根据id号获取文章")
    @RequestMapping(value = "/informpost", method = RequestMethod.GET)
    public JSONResult getPostById(String postId, String openId) throws Exception{
        Post post = communityService.selectByPostId(Integer.parseInt(postId));
        PostVO postVO = new PostVO();
        BeanUtils.copyProperties(post, postVO);

        User user = userService.selectById(openId);
        postVO.setAuthorName(user.getNickname());
        postVO.setAvatarImg(user.getAvatar());


        CommentsExample commentsExample = new CommentsExample();
        CommentsExample.Criteria criteria2 = commentsExample.createCriteria();
        criteria2.andPostIdEqualTo(Integer.parseInt(postId));
        List<Comments> commentsList = communityService.selectCommentsByExample(commentsExample);
        postVO.setCommentsNum(commentsList.size());
        List<CommentsVO> commentsVOList = new ArrayList<>();
        for(int j = 0; j < commentsList.size(); ++j){
            CommentsVO commentsVO = new CommentsVO();
            BeanUtils.copyProperties(commentsList.get(j), commentsVO);
            User commentUser = userService.selectById(commentsList.get(j).getUserId());
            commentsVO.setUserName(commentUser.getNickname());
            commentsVO.setUserAvatarUrl(commentUser.getAvatar());
            commentsVO.setLiked(0);
            commentsVOList.add(commentsVO);
        }
        postVO.setCommentsList(commentsVOList);
        return JSONResult.ok(postVO);
    }

    @ApiOperation(value="保存评论",notes = "保存用户在帖子下面的留言")
    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    public JSONResult saveComment(String openId, String commentText, String postId) throws Exception{

        Comments comments = new Comments();
        comments.setCreateTime(new Date());
        comments.setUserId(openId);
        comments.setCommentText(commentText);
        comments.setLikeNum(0);
        comments.setPostId(Integer.parseInt(postId));
        communityService.insertComments(comments);
        return JSONResult.ok();
    }

    @ApiOperation(value="阅读量自增",notes = "帖子的阅读量加1")
    @RequestMapping(value = "/read", method = RequestMethod.GET)
    public JSONResult increaseReadNum(String postId) throws Exception{

        Post post = new Post();
        post.setId(Integer.parseInt(postId));
        Post target = communityService.selectByPostId(Integer.parseInt(postId));
        post.setReadNum(target.getReadNum() + 1);
        communityService.updatePostSelective(post);
        return JSONResult.ok();
    }
    @ApiOperation(value="搜索帖子",notes = "模糊搜索帖子")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public JSONResult searchPost(String key) throws Exception{

        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria();
        criteria.andPostTextLike("%"+key+"%");
        criteria.andSubtitleLike("%"+key+"%");

        return JSONResult.ok(communityService.selectByExample(postExample));
    }

    @ApiOperation(value="获取评论",notes = "通过某个帖子的id号获取其评论")
    @RequestMapping(value = "/getcomment", method = RequestMethod.GET)
    public JSONResult getComments(String postId) throws Exception{

        CommentsExample commentsExample = new CommentsExample();
        CommentsExample.Criteria criteria = commentsExample.createCriteria();
        criteria.andPostIdEqualTo(Integer.parseInt(postId));
        List<Comments> commentsList = communityService.selectCommentsByExample(commentsExample);

        List<CommentsVO> commentsVOList = new ArrayList<>();
        for(int j = 0; j < commentsList.size(); ++j){
            CommentsVO commentsVO = new CommentsVO();
            BeanUtils.copyProperties(commentsList.get(j), commentsVO);
            User commentUser = userService.selectById(commentsList.get(j).getUserId());
            commentsVO.setUserName(commentUser.getNickname());
            commentsVO.setUserAvatarUrl(commentUser.getAvatar());
            commentsVO.setLiked(0);
            commentsVOList.add(commentsVO);
        }

        return JSONResult.ok(commentsVOList);
    }
    @ApiOperation(value="点赞评论",notes = "点赞评论时点赞数加1")
    @RequestMapping(value = "/like", method = RequestMethod.GET)
    public JSONResult likeComments(String commentId, String likeNum) throws Exception{

        Comments comments = new Comments();
        comments.setId(Integer.parseInt(commentId));
        comments.setLikeNum(Integer.parseInt(likeNum));
        communityService.updateCommentsSelective(comments);
        return JSONResult.ok();
    }



}
